home *** CD-ROM | disk | FTP | other *** search
- /*-------------------------------------------------------------------
-
- AOCE Post Office Protocol (POP) / Simple Mail Transfer Protocol (SMTP)
- Mail Service Access Module
-
- written by Steve Falkenburg-- MacDTS
- ©1991-1993 Apple Computer, Inc.
-
- --------------
- change history
- --------------
-
- SJF 02/19/93 update for beta build b1
- SJF 10/29/92 update to a11 a11
- SJF 06/08/92 update to a8 a8
- SJF 02/15/92 first working version a4.5
- SJF 10/16/91 initial coding a3
-
- ---------------------------------------------------------------------*/
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __SEGLOAD__
- #include <SegLoad.h>
- #endif
-
- #ifndef __TRAPS__
- #include <Traps.h>
- #endif
-
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
-
- #ifndef __EPPC_
- #include <EPPC.h>
- #endif
-
- #ifdef applec
- #include <SysEqu.h>
- #endif
-
- #include "const.h"
- #include "gwerrors.h"
- #include "globals.h"
- #include "network.h"
- #include "gatewaystuff.h"
- #include "trapavailable.h"
- #include "utils.h"
- #include "myevents.h"
- #include "spoolsystem.h"
- #include "authstuff.h"
- #include "gatewayevents.h"
- #include "main.h"
-
- #ifdef __SYSEQU__
- #define DefaultStackSize *((long *)DefltStack)
- #define StackBase *((Ptr *)CurStackBase)
- #else
- #define DefaultStackSize DefltStack
- #define StackBase CurStackBase
- #endif
-
- // main
- //
- // main entry point- initializes program components and
- // calls main event loop. exits via ExitToShell if gateway can't initialize
- //
- void main(void)
- {
- OSErr err;
-
- TraceExecution("\pGATEWAY LAUNCH");
-
- if (!HasAOCE())
- ExitToShell();
-
- err = InitMacStuff();
- if (err!=noErr) {
- DoError(err);
- ExitToShell();
- }
-
- err = InitSpoolSubsystem();
- if (err!=noErr) {
- DoError(err);
- ExitToShell();
- }
-
- gNetworkInited = false;
- InitRemoteNetStuff();
- // don't exit if we get an error, we'll run anyway, but won't route mail
-
- err = InitAOCEStuff();
- if (err!=noErr) {
- DoError(err);
- ExitToShell();
- }
-
- err = InitGatewayStuff();
- if (err!=noErr) {
- DoError(err);
- ExitToShell();
- }
-
- MainLoop();
- ExitProc();
- }
-
-
- // ExitProc
- //
- // called before the gateway terminates. this call insures that everything associated
- // with AOCE and external networking stuff is closed and released.
- //
- void ExitProc(void)
- {
- TraceExecution("\pGATEWAY QUIT");
-
- CloseGatewayStuff();
- CloseRemoteNetStuff();
- CloseAOCEStuff();
-
- ExitToShell();
- }
-
-
- // InitMacStuff
- //
- // called to initialize the Mac toolbox stuff required for the gateway, which is a background
- // only application.
- //
- OSErr InitMacStuff(void)
- {
- SetApplLimit((Ptr) ((long) StackBase - (long) 0x6000)); // workaround for DefltStackSize lomem being munged
- MaxApplZone();
- MoreMasters();
- MoreMasters();
- InitGraf(&qd.thePort); // need this to generate random numbers
-
- gAOCEInited = false;
- gDone = false;
- gWakeUp = gWakeUpSecondary = false;
-
- if (!TrapAvailable(_WaitNextEvent))
- return kCantRun;
-
- if (!SupportsAEVT())
- return kCantRun;
-
- GetCurrentProcess(&gOurPSN);
-
- return noErr;
- }
-
-
- // MainLoop
- //
- // this is the main event loop for the gateway application. the sleep time will be set to the
- // maximum value (-1L) so that we never take any idle time and only wake up when we're sent
- // an event.
- //
- // *** SPECIAL NOTE:
- //
- // it's not strictly necessary to have this event loop at all. the toolbox will re-launch the
- // gateway whenenver it actually needs to have the gateway to something. i've chosen to keep the
- // gateway running at all times, since i read in the slot information into a global area and also
- // to insure that i will always have enough memory to activate. this is probably not the optimal
- // strategy, but it is only sample code...
- //
- void MainLoop(void)
- {
- EventRecord ev;
- Boolean gotEvt;
- OSErr err;
-
- while (gDone==false) {
-
- // see if we need to refresh slot info
- err = CheckSlotRefresh();
- if (err!=noErr)
- DoError(err);
-
- // see if we need to refresh identity stuff (in response to auth q notification)
- err = CheckAuthRefresh();
- if (err!=noErr)
- DoError(err);
-
- err = ProcessQueuedEvents();
- if (err!=noErr)
- DoError(err);
-
- gotEvt = false;
- if (gWakeUp==false)
- gotEvt = WaitNextEvent(everyEvent,&ev,kSleepTime,nil);
- gWakeUp = false;
- if (gotEvt) {
- err = HandleEvent(&ev,true);
- if (err!=noErr)
- DoError(err);
- }
- }
- }
-
-
-
- // SecondaryEventHandler
- //
- // this event handler is what we spin on when waiting for an async call to complete
- // we will process a subset of the EPPCs from here, namely: CreateSlot, ModifySlot, DeleteSlot
- // all other EPPCs are queued onto a waiting to be processed event queue
- // (not re-entrant)
- //
- void SecondaryEventLoop(void)
- {
- EventRecord ev;
- Boolean gotEvt;
- OSErr err;
-
- gotEvt = false;
-
- if (gWakeUpSecondary==false) {
- gotEvt = WaitNextEvent(everyEvent,&ev,kSecondarySleepTime,nil);
- }
-
- if (gotEvt) {
- err = HandleEvent(&ev,false);
- if (err!=noErr)
- DoError(err);
- }
- }
-